package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 * @Author Pan Rundong
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info(setmealDto.toString());
        setmealService.saveWithDish(setmealDto);
        return R.success("保存套餐成功");
    }

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        /*//构造分页对象
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        Page<SetmealDto> pageDto = new Page<>();

        //设置查询条件
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Setmeal::getName,name);
        //设置排序条件
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        //查询
        setmealService.page(pageInfo,queryWrapper);

        //拷贝
        BeanUtils.copyProperties(pageInfo,pageDto,"records");

        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list = records.stream().map((item)->{
            //创建SetmealDTO对象
            SetmealDto setmealDto = new SetmealDto();
            //拷贝
            BeanUtils.copyProperties(item,setmealDto);
            //获取分类id
            Long categoryId = item.getCategoryId();
            //获取分类对象
            Category category = categoryService.getById(categoryId);
            if(category != null){
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());

        pageDto.setRecords(list);
*/
        Page<SetmealDto> pageDto = setmealService.pageBySql(page, pageSize, name);

        return R.success(pageDto);
    }

    /**
     * 套餐删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info(ids.toString());
        setmealService.deleteWithDish(ids);
        return R.success("套餐删除成功");
    }

    /**
     * 根据套餐id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id){
        log.info("获取套餐id，{}",id);
        SetmealDto setmealDto = setmealService.getByIdWithDish(id);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐信息
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        log.info(setmealDto.toString());
        setmealService.updateWithDish(setmealDto);
        return R.success("修改套餐成功");
    }

    /**
     * 套餐起售停售
     * @param status
     * @param ids
     * @return R(String)
     */
    @PostMapping("/status/{status}")
    public R<String> changeStatus(@PathVariable Integer status, @RequestParam List<Long> ids){
        log.info("status为{}",status);
        log.info("ids为{}",ids.toString());
        //传入需要改变的setmeal的id以及status
        setmealService.changeStatus(status,ids);

        //区分返回信息
        String msg = "";
        if(ids.size() == 1){
            msg = "状态修改成功";
        }else {
            msg = "状态批量修改成功";
        }
        return R.success(msg);
    }

    /**
     * 根据条件查询setmeal
     * @Autoer Pan Rundong
     * @Date 2023-01-31 17:35
     *   (○` 3′○)~~♡   (°ー°〃)
     * @param setmeal
     * @return com.itheima.reggie.common.R<java.util.List < com.itheima.reggie.entity.Setmeal>>
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        //设置条件构造器查询对应setmeal
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //根据categoryId查询
        queryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId,setmeal.getCategoryId());
        //根据status查询
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        //根据更新时间排序
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        //查询
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 根据套餐id查询菜品
     * @Author: Pan Rundong
     * @Date: 2023-02-05 19:15
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param id
     * @return com.itheima.reggie.common.R<com.itheima.reggie.dto.DishDto>
     */
    @GetMapping("/dish/{id}")
    public R<SetmealDto> getDishById(@PathVariable Long id) {
        log.info("id为：{}",id);
        SetmealDto setmealDto = setmealService.getByIdWithDish(id);
//        DishDto dishDto = dishService.getByIdWithFlavor()
        return R.success(setmealDto);
    }
}
